package pCloud.client;

import pCloud.PCloudFactory;
import pCloud.config.PCloudConfig;
import pCloud.service.CachingHandle;
import pCloud.service.IncompleteResponse;
import pCloud.service.ProtocolHandle;
import pCloud.service.ProxyServerDataServerProtHandle;
import pCloud.transport.Communicator;
import pCloud.transport.EndPointTransportHandle;

public class ProxyServerDataServerConnector extends Connector {

	CachingHandle cacheHandler;
	IncompleteResponse incompleteResponses;

	public ProxyServerDataServerConnector(String remoteHost, int port,
			Communicator comm, CachingHandle cache, IncompleteResponse ir)
			throws Exception {
		super(remoteHost, port, comm);
		this.cacheHandler = cache;
		this.incompleteResponses = ir;
	}

	@Override
	public EndPointTransportHandle getNewClientTransportHandle(String host,
			int port) throws Exception {
		EndPointTransportHandle ep = PCloudFactory
				.getEndPointTransportHandle(PCloudConfig
						.getString(PCloudConfig.PCLOUD_TRANSPORT_CLIENT));
		ep.initalize(host, port);
		return ep;
	}

	@Override
	public ProtocolHandle getNewProtocolHandle() throws Exception {
		ProxyServerDataServerProtHandle ph = (ProxyServerDataServerProtHandle) PCloudFactory
				.getNewProtocolHandleInstance(PCloudConfig
						.getString(PCloudConfig.PCLOUD_PROXY_SERVER_CLIENT_PROT));

		if (PCloudConfig.containsField(PCloudConfig.PCLOUD_CACHE_OPTION)
				&& PCloudConfig.getBoolean(PCloudConfig.PCLOUD_CACHE_OPTION))
			ph.initializeCacheHandler(this.cacheHandler);

		ph.initializeIncompleteResponse(this.incompleteResponses);

		return ph;
	}

}
